package donjuan;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
//在用户选择除添加以外的三个选项时，会隐藏之前的创就并临时创建该窗口，数据展示采用JTable模板，以模仿数据表格
public class Window_decide extends JFrame implements ActionListener {
  JTable table;
  
  Object[][] a;
  
  Object[] name = new Object[] { "账户", "密码", "网址", "信息", "备注"};
  
  JButton back;
  
  JLabel hint;
  
  int mode = 0;
  
  ArrayList<Integer> line = null;
  
  private String sign;
  
  LinkCellRenderer renderer;
  
  Window_decide(ArrayList<Integer> line, int mode, String sign) 
  {
	this.sign = sign;
    this.line = line;
    this.mode = mode;
    setLayout(new BorderLayout());
    this.a = new Object[line.size()][5];
    int i = 0;
	   for (Iterator<Integer> iterator = line.iterator(); iterator.hasNext(); ) 
	    {
	      int k = iterator.next();
	      for (int j = 0; j < 5; j++)
	        this.a[i][j] = ((Readin)Message.read.get(k)).data[j]; 
	      i++;
	    } 
   
    this.back = new JButton(set_button_name());
    this.back.addActionListener(this);
    this.table = new JTable(this.a, this.name);
    this.table.setRowHeight(this.table.getRowHeight() + 10);
    show_link();
    if(mode==1||mode==2)
    	add(new JScrollPane(this.table), "Center");
    else
    	getContentPane().add(new JScrollPane(renderer.table), "Center");
    this.hint = new JLabel("模式");
    add(this.hint, "North");
    add(this.back, "South");
    setSize(800, 500);
    setVisible(true);
    back.setBorder(BorderFactory.createRaisedBevelBorder());
    back.setFont(new Font("华文新魏",1,15));
  }
  
  private String set_button_name() {
    if (this.mode == 1)
      return "删除"; 
    if (this.mode == 2)
      return "更新"; 
    return "返回";
  }
  private void show_link()
  {
	  if(mode!=2&&mode!=1)
	  {	
		  	renderer = new LinkCellRenderer(this.table);
		  	renderer.table.setDefaultRenderer(Object.class, renderer);
		  	renderer.table.addMouseListener(renderer);
		  	renderer.table.addMouseMotionListener(renderer);
	  }
  }
  private String[] Obj_to_String(int j) {
    String[] temp = new String[5];
    for (int i = 0; i < 5; i++)
      temp[i] = this.a[j][i].toString(); 
    return temp;
  }
  
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.back) {
      if (this.mode == 2) 
      {
        for (int i = 0; i < this.a.length; i++) 
        {
          for (int k = 0; k < 5; k++) 
          {String str= this.a[i][k].toString();}
        } 
        int j = 0;
        for (Iterator<Integer> iterator = this.line.iterator(); iterator.hasNext(); ) {
          int k = ((Integer)iterator.next()).intValue();
          Readin.refresh_data(Message.read, Obj_to_String(j), Message.con, k);
          j++;
        } 
      } 
      else if (this.mode == 1) 
      {
        Readin.del_data(Message.read, this.sign, Message.state, Message.con, Dialog_mode.mode);
      } 
      Window_main.mode = 6;
      dispose();
      Window_main.jump();
      Message.show.setVisible(true);
    } 
  }
}
